home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Magazine / ActiveARexx / NetPBM-WOS / src / libopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-14  |  6.3 KB  |  178 lines

  1. /*----------------------------------------------------------------------------
  2.                                  libopt
  3. ------------------------------------------------------------------------------
  4.   This is a program to convert link library filepaths to linker options
  5.   that select them.  E.g. ../pbm/libpbm.so becomes -L../pbm -lpbm   .
  6.  
  7.   Each argument is a library filepath.  The option string to identify
  8.   all of those library filepaths goes to Standard Output.
  9.  
  10.   If an argument doesn't make sense as a library filespec, it is
  11.   copied verbatim, blank delimited, to the output string.
  12.   
  13.   There is no newline or null character or anything after the output.
  14. ------------------------------------------------------------------------------
  15.   Why would you want to use this?  
  16.  
  17.   On some systems, the -L/-l output of this program has exactly the
  18.   same effect as the filepath input when used in the arguments to a
  19.   link command.  A GNU/Linux system, for example.  On others (Solaris,
  20.   for example), if you include /tmp/lib/libpbm.so in the link as a
  21.   link object, the executable gets built in such a way that the system
  22.   accesses the shared library /tmp/lib/libpbm.so at run time.  On the
  23.   other hand, if you instead put the options -L/tmp/lib -lpbm on the
  24.   link command, the executable gets built so that the system accesses
  25.   libpbm.so in its actual installed directory at runtime (that
  26.   location might be determined by a --rpath linker option or a
  27.   LD_LIBRARY_PATH environment variable at run time).
  28.  
  29.   In a make file, it is nice to use the same variable as the
  30.   dependency of rule that builds an executable and as the thing that
  31.   the rule's command uses to identify its input.  Here is an example
  32.   of using libopt for that:
  33.  
  34.      PBMLIB=../pbm/libpbm.so
  35.      ...
  36.      pbmmake: pbmmake.o $(PBMLIB)
  37.              ld -o pbmmake pbmmake.o `libopt $(PBMLIB)` --rpath=/lib/netpbm
  38.  
  39.   Caveat: "-L../pbm -lpbm" is NOT exactly the same as "libpbm.so" on any
  40.   system.  All of the -l libraries are searched for in all of the -L
  41.   directories.  So you just might get a different library with the -L/-l
  42.   version than if you specify the library file explicitly.
  43.  
  44. -----------------------------------------------------------------------------*/
  45. #define _BSD_SOURCE 1
  46.     /* Make sure strdup() is in stdio.h */
  47.  
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51.  
  52. typedef unsigned char bool;
  53. #define true (1)
  54. #define false (0)
  55.  
  56. static void
  57. parse_filename(const char * const filename,
  58.                char ** const libname_p,
  59.                bool * const valid_library_p) {
  60. /*----------------------------------------------------------------------------
  61.    Extract the library name component of the filename 'filename'.  This
  62.    is just a filename, not a whole pathname.
  63.  
  64.    Return it in newly malloc'ed storage pointed to by '*libname_p'.
  65.    
  66.    E.g. for "libxyz.so", return "xyz".
  67.  
  68.    return *valid_library == true iff 'filename' validly names a library
  69.    that can be expressed in a -l linker option.
  70. -----------------------------------------------------------------------------*/
  71.     char *lastdot;  
  72.     /* Pointer to last period in 'filename'.  Null if none */
  73.     
  74.     /* We accept any period-delimited suffix as a library type suffix.
  75.            It's probably .so or .a, but is could be .kalamazoo for all we
  76.            care.  
  77.         */
  78.     lastdot = strrchr(filename, '.');
  79.     if (lastdot == NULL) {
  80.         /* This filename doesn't have any suffix, so we don't understand
  81.            it as a library filename.
  82.            */
  83.         *valid_library_p = false;
  84.     } else {
  85.         if (strncmp(filename, "lib", 3) != 0) {
  86.             /* This filename doesn't start with "lib", so we can't express
  87.                it as a -l option.
  88.             */
  89.             *valid_library_p = false;
  90.         } else {
  91.             /* Extract everything between "lib" and "." as 
  92.                    the library name 
  93.                 */
  94.             *libname_p = strdup(filename+3);
  95.             (*libname_p)[lastdot - filename - 3] = '\0';
  96.         
  97.             if (strlen(*libname_p) == 0)
  98.                 *valid_library_p = false;
  99.             else
  100.                 *valid_library_p = true;
  101.         }
  102.     }
  103. }   
  104.  
  105.  
  106.  
  107. static void
  108. parse_filepath(const char * const filepath,
  109.                char ** const directory_p, char ** const filename_p) {
  110. /*----------------------------------------------------------------------------
  111.    Extract the directory and filename components of the filepath 
  112.    'filepath' and return them in newly malloc'ed storage pointed to by
  113.    '*directory_p' and '*filename_p'.
  114. -----------------------------------------------------------------------------*/
  115.     char *lastslash; /* Pointer to last slash in 'filepath', or null if none */
  116.  
  117.     lastslash = strrchr(filepath, '/');
  118.  
  119.     if (lastslash == NULL) {
  120.         /* There's no directory component; the filename starts at the
  121.            beginning of the filepath 
  122.         */
  123.         *filename_p = strdup(filepath);
  124.         *directory_p = strdup("");
  125.     } else {
  126.         /* Split the string at the slash we just found, into filename and 
  127.            directory 
  128.            */
  129.         *filename_p = strdup(lastslash+1);
  130.         *directory_p = strdup(filepath);
  131.         (*directory_p)[lastslash - filepath] = '\0';
  132.     }
  133. }
  134.  
  135.  
  136.  
  137. int
  138. main(int argc, char **argv) {
  139.  
  140.     unsigned int arg;  /* Index into argv[] of argument we're processing */
  141.  
  142.     for (arg = 1; arg < argc; arg++) {
  143.         const char *filepath;  /* The argument we're processing */
  144.         bool valid_library;  
  145.           /* Our argument is a valid library filepath that can be converted to
  146.              -l/-L notation.
  147.           */
  148.         char *directory;
  149.           /* Directory component of 'filepath' */
  150.         char *filename;
  151.           /* Filename component of 'filepath' */
  152.         char *libname;
  153.           /* Library name component of 'filename'.  e.g. xyz in libxyz.so */
  154.  
  155.         filepath = argv[arg];
  156.  
  157.         parse_filepath(filepath, &directory, &filename);
  158.  
  159.         parse_filename(filename, &libname, &valid_library);
  160.  
  161.         if (valid_library) {
  162.             if (strlen(directory) == 0)
  163.                 fprintf(stdout, "-L. -l%s ", libname);
  164.             else
  165.                 fprintf(stdout, "-L%s -l%s ", directory, libname);
  166.         } else
  167.             fprintf(stdout, "%s ", filepath);
  168.     
  169.         free(directory); free(filename); free(libname);
  170.     }
  171.     return 0;
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178.